package cn.alpha.intell.rbtlib.wifi.dlalog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.rbt.utils.ButtonSound;


/***
 * @author jinlongfeng
 * @desc 手动添加 wifi Dialog
 */
public class AddWifiDialog extends Dialog {

    private static final String TAG = "AddWifiDialog";

    public AddWifiDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void applyCompat() {
        if (Build.VERSION.SDK_INT < 19) {
            return;
        }
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        applyCompat();
        super.onCreate(savedInstanceState);
    }

    public interface ClickClickListener {
        void onClick(View view, String wifiName, String wifiType, String wifiPaw);
    }

    public interface TextChangedListener {
        void ChangedListener(int count);
    }

    public static class Builder {
        private Context context;
        private String wifitype;
        private EditText editName, editPwd;
        private Spinner mSpinner;
        private TextView mTextViewPwd;
        private Button addBtn;
        private ClickClickListener addBtnClickListener;
        private TextChangedListener textChangedListener;

        private boolean isNoPasswd;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * set the dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {

            return this;
        }

        /**
         * set the dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            return this;
        }

        /**
         * Set the Dialog content from String
         *
         * @param content
         * @return
         */
        public Builder setMessage(String content) {

            return this;
        }

        /**
         * Set the Dialog content from resource
         *
         * @param content
         * @return
         */
        public Builder setMessage(int content) {
            return this;
        }

        /**
         * Set the negative button String and it's listener
         *
         * @param cancelText
         * @param cancelClickListener
         * @return
         */
        public Builder setCancelText(String cancelText, OnClickListener cancelClickListener) {
            return this;
        }


        public Builder setAddBtn(ClickClickListener onClickListener) {
            this.addBtnClickListener = onClickListener;
            return this;
        }

        public Builder textChangedListener(TextChangedListener textChangedListener) {
            this.textChangedListener = textChangedListener;
            return this;
        }

        public AddWifiDialog create() {

            final AddWifiDialog dialog = new AddWifiDialog(context, R.style.Dialog);
            LayoutInflater inflater = dialog.getLayoutInflater();
            final View view = inflater.inflate(R.layout.dailog_add_wifi, null);
            ImageView addImageViewClose = (ImageView) view.findViewById(R.id.add_wifi_close);
            addBtn = (Button) view.findViewById(R.id.add_btn);
            mSpinner = (Spinner) view.findViewById(R.id.add_wifi_item_spinner);
            editName = (EditText) view.findViewById(R.id.add_wifi_name_edit_item);
            editPwd = (EditText) view.findViewById(R.id.add_wifi_paw_edit);
            mTextViewPwd = (TextView) view.findViewById(R.id.add_wifi_paw);
            addBtn.setEnabled(false);
            addBtn.setBackgroundResource(R.drawable.wifi_conn_disable);

            editPwd.addTextChangedListener(new EditTextTextChangedListener(false));
            editName.addTextChangedListener(new EditTextTextChangedListener(true));

            //资源转[]
            final String meinv[] = context.getResources().getStringArray(R.array.wifitype);
            //构造ArrayAdapter
            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                    R.layout.simple_spinner_item, meinv);
            //设置下拉样式以后显示的样式
            adapter.setDropDownViewResource(R.layout.my_drop_down_item);
            mSpinner.setAdapter(adapter);
            mSpinner.setSelection(0);

            mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    wifitype = meinv[position];
                    //无密码
                    if (wifitype.equals(meinv[0])) {
                        mTextViewPwd.setVisibility(View.GONE);
                        editPwd.setVisibility(View.GONE);
                        isNoPasswd = true;
                        String ssid = editName.getText().toString().trim();
                        if (!TextUtils.isEmpty(ssid)) {
                            setBtnEnable(true);
                        }

                    } else {
                        isNoPasswd = false;
                        mTextViewPwd.setVisibility(View.VISIBLE);
                        editPwd.setVisibility(View.VISIBLE);

                        if(isSSIDTure()&&isPasswdTure()){
                            setBtnEnable(true);
                        }else {
                            setBtnEnable(false);
                        }
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    wifitype = meinv[0];
                }
            });

            dialog.addContentView(view, new LinearLayout.LayoutParams(717, 565));
            addImageViewClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            addBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "setOnClickListener");

                    ButtonSound.playOgg(context);
                    String ssid = editName.getText().toString().trim();
                    String pwd = editPwd.getText().toString().trim();
                    String type = wifitype;
                    //无密码
                    if (type.equals(meinv[0])) {
                        if (addBtnClickListener != null) {
                            addBtnClickListener.onClick(null, ssid, type, pwd);
                            dialog.dismiss();
                        }
                        //有密码
                    } else {
                        if (!(TextUtils.isEmpty(ssid) || TextUtils.isEmpty(pwd))) {
                            if (addBtnClickListener != null) {
                                addBtnClickListener.onClick(null, ssid, type, pwd);
                                dialog.dismiss();
                            }
                        }
                    }
                }
            });

            dialog.setCanceledOnTouchOutside(false);
            return dialog;
        }


        private boolean isSSIDTure(){
            return !TextUtils.isEmpty(editName.getText().toString());
        }

        private boolean isPasswdTure(){

            String pwd = editPwd.getText().toString();

            if(TextUtils.isEmpty(pwd) || pwd.length()<7 ){
                return false;
            }else {
                return true;
            }
        }

        private void setBtnEnable(boolean isEnable) {
            if (isEnable) {
                addBtn.setEnabled(true);
                addBtn.setBackgroundResource(R.drawable.selector_wifi_btn_conn);

            } else {
                addBtn.setEnabled(false);
                addBtn.setBackgroundResource(R.drawable.wifi_conn_disable);
            }
        }

        private class EditTextTextChangedListener implements TextWatcher {

            private boolean isSSIDName;

            public EditTextTextChangedListener(boolean isSSIDName) {
                this.isSSIDName = isSSIDName;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 无密码
                if (isNoPasswd) {
                    //监听 SSID 输入
                    if (isSSIDName) {
                        if (s.length() > 1) {
                            setBtnEnable(true);
                        } else {
                            setBtnEnable(false);
                        }
                        // 监听 pwssd 输入
                    }else {


                    }

                    //有密码
                } else {

                    // 监听 SSID 输入
                    if(isSSIDName){

                        if(isPasswdTure()&&s.length()>1){
                            setBtnEnable(true);
                        }else {
                            setBtnEnable(false);
                        }
                        // 监听 passwd 输入
                    }else {

                        if(isSSIDTure()&&s.length()>7){
                            setBtnEnable(true);
                        }else {
                            setBtnEnable(false);
                        }

                    }
                }

            }
        }

    }
}

